package com.lethelh.mgallery.dao;

import java.util.ArrayList;
import java.util.List;

import com.lethelh.mgallery.entity.Painting;
import com.lethelh.mgallery.utils.PageModel;
import com.lethelh.mgallery.utils.XmlDataSource;
/**
 * 油画数据访问对象
 * @author LetheLH
 *
 */
public class PaintingDao {
	/**
	 * 分页查询油画数据
	 * @param page 页号
	 * @param rows 每页记录数
	 * @return PageModel对象
	 */
	public PageModel pagination(int page,int rows) {
		//Painting油画对象集合
		List<Painting> list = XmlDataSource.getRawData();
		//PageModel分页处理得到分页数据及分页附加
		PageModel pageModel = new PageModel(list,page,rows);
		return pageModel;
	}
	/**
	 * 按分类保存油画数据
	 * @param category
	 * @param page
	 * @param rows
	 * @return
	 */
	public PageModel pagination(int category,int page,int rows) {
		List<Painting> list = XmlDataSource.getRawData();
		List<Painting> categoryList = new ArrayList<Painting>();
		for(Painting p:list) {
			if(p.getCategory() == category) {
				categoryList.add(p);
			}
		}
		PageModel pageModel = new PageModel(categoryList, page, rows);
		return pageModel;
	}
	/**
	 * 数据新增
	 * @param painting
	 */
	public void create(Painting painting) {
		XmlDataSource.append(painting);
	}
	
	/**
	 * 数据更新
	 * @param painting
	 */
	public void update(Painting painting) {
		XmlDataSource.update(painting);
	}
	/**
	 * 查找油画
	 * @param id
	 * @return
	 */
	public Painting findById(Integer id) {
		List<Painting> data = XmlDataSource.getRawData();
		Painting painting = null;
		for (Painting p : data) {
			if(p.getId() == id) {
				painting = p;
				break;
			}
		}
		return painting;
	}
	/**
	 * 删除油画
	 * @param id
	 */
	public void delete(Integer id) {
		XmlDataSource.delete(id);
	}
}
